/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import test.hibernate.dao.CustomerDao;
import test.hibernate.pojo.Customer;

/**
 *
 * @author mamat
 */
public class AddCustomerController extends MultiActionController{

    public ModelAndView add(HttpServletRequest arg0, HttpServletResponse arg1, Customer customer) throws Exception {
        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute(new Customer());
        System.out.println(">>>cek customer.getFirstname() :"+customer.getFirstname());
        System.out.println(">>>cek customer.getLastname() :"+customer.getLastname());
        System.out.println(">>>cek customer.getAge() :"+customer.getAge());

        String action = arg0.getParameter("action")==null?"":arg0.getParameter("action");
        if(action.equalsIgnoreCase("1")){
            CustomerDao customerDao = (CustomerDao) getApplicationContext().getBean("CustomerDao");
            int id = customerDao.getLastId()+1;
            System.out.println(">>>LastID.next:"+id);
            customer.setId(id);
            customerDao.addCustomer(customer);
            return new ModelAndView("redirect:list.htm");
        }else {
            return new ModelAndView("add",modelMap);
        }
    }
}
